<template>
  <div class="tab-pane fade" :id="id" role="tabpanel">
    <div class="card col-12">
      <div class="card-body">
        <form id="frmVaasConfig" class="form-horizontal">
          <div class="row">
            <label class="control-label col-2 ms-auto text-end"
              >VAAS接口网址</label
            >
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.number="vaasUrl"
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end">用户名</label>
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="vaasUsername"
              />
            </div>
          </div>
          <div class="row">
            <label class="control-label col-2 ms-auto text-end"
              >secretKey</label
            >
            <div class="col-4 me-auto">
              <input
                type="input"
                class="form-control"
                v-model.trim="vaasSecretKey"
              />
              <span class="text-muted small">留空白，表示不修改</span>
            </div>
          </div>
          <div class="row">
            机场服务商网址：
            <a href="http://vaas.90sky.com/" target="_blank"
              >http://vaas.90sky.com/</a
            >
          </div>
          <div class="row">
            <button
              id="btnChange"
              type="button"
              class="btn btn-primary  col-4 offset-4"
              :disabled="saving"
              @click.stop="saveEntVaasConfig()"
            >
              保存
            </button>
          </div>
        </form>
      </div>
    </div>
  </div>
</template>

<script>
import { searchEntVaasConfig, saveEntVaasConfig } from "@/api/admin.js";

export default {
  props: {
    id: {
      type: String,
    },
  },
  data() {
    return {
      vaasUrl: "",
      vaasUsername: "",
      vaasSecretKey: "",

      saving: false,
    };
  },
  mounted: function () {
    this.searchVaasConfig();
  },
  methods: {
    showErrMsg: function (msg, msgType) {
      this.$store.dispatch("showAlertMsg", {
        errMsg: msg,
        errMsgType: msgType,
      });
    },
    searchVaasConfig: function () {
      searchEntVaasConfig((v) => {
        this.vaasUrl = v.url;
        this.vaasUsername = v.username;
        this.vaasSecretKey = v.secretKey;
      });
    },
    saveEntVaasConfig: function () {
      const params = {
        url: this.vaasUrl,
        username: this.vaasUsername,
        secretKey: this.vaasSecretKey,
      };

      this.saving = true;

      saveEntVaasConfig(
        params,
        (v) => {
          if (v.status === "OK") this.showErrMsg("保存成功");
          else this.showErrMsg(v.errmsg, "danger");
        },
        () => {
          this.saving = false;
        }
      );
    },
  },
};
</script>